package com.tanhua.management.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.QuestionBankApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Options;
import com.tanhua.domain.mongo.Questionnaire;
import com.tanhua.domain.mongo.Subject;
import com.tanhua.domain.vo.AllQuestionsVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.QuestionsVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InfoService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private QuestionBankApi questionBankApi;


    public PageBeanVo getUsers(Integer page, Integer pageSize) {

        IPage<UserInfo> ipage = userInfoApi.findUsers(page, pageSize);

        PageBeanVo pageBeanVo = new PageBeanVo(page, pageSize, ipage.getTotal(), ipage.getRecords());

        return pageBeanVo;
    }

    public List<Questionnaire> getQuestionnaire() {
        return userInfoApi.getQuestionnaire();
    }

    public void createQuestionnaire(QuestionsVo questionsVo) {

        Questionnaire questionnaire = questionBankApi.createQuestionnaire(questionsVo);

        String content = questionsVo.getContent();
        String[] split = content.split("=");

        for (String single : split) {
             if("".equals(single)){
                 continue;
             }
             //问题
            String[] question = single.split("/");
            Subject subject = new Subject();
            subject.setQuestion(question[0]);
            subject.setQuestionId(questionnaire.getQuestionId());
            subject.setStar(questionsVo.getStarNum());
            subject.setTitleId(questionnaire.getQuestionId());
            Subject sub = questionBankApi.insertSubject(subject);

            for (int i = 1; i < question.length; i+=2) {
                Options options = new Options();
                options.setOption(question[i]);     //选项内容
                options.setScore(Integer.parseInt(question[i+1].replaceAll("\n","").replaceAll("分","")));   //分
                options.setQuestionId(questionnaire.getQuestionId());
                options.setTitleId(sub.getTitleId());
                Options opt = questionBankApi.inserOptions(options);
            }
        }


    }

    public AllQuestionsVo getQuestions(String id) {
        AllQuestionsVo allQuestionsVo = new AllQuestionsVo();
        Questionnaire questionnaire = questionBankApi.getQuestionnaire(id);
        allQuestionsVo.setTitle(questionnaire.getName());
        allQuestionsVo.setLevelStr(questionnaire.getLevel());
        allQuestionsVo.setStarNum(questionnaire.getStar());
        allQuestionsVo.setPicSrc(questionnaire.getCover());
        allQuestionsVo.setQuestionId(questionnaire.getQuestionId());

        StringBuilder content = new StringBuilder();

        List<Subject> subjects = questionBankApi.getQuestions(id);
        for (Subject subject : subjects) {
            content.append("="+subject.getQuestion()+"\n");

            String titleId = subject.getTitleId();
            List<Options> options = questionBankApi.getOptions(titleId);
            for (Options option : options) {
                content.append("/"+option.getOption()+"/"+option.getScore()+"\n");
            }
        }

        allQuestionsVo.setContent(content.toString());
        return allQuestionsVo;
    }
}
